﻿#include "Hashbucket.h"

namespace Ysz
{
	template<class T>
	struct KeyOfT
	{
		const T& operator()(const T& data)
		{
			return data;
		}

	};

	template<class K>
	struct Hash
	{
		size_t operator()(const K& key)
		{
			return (size_t)key;
		}

	};
	template<>

	struct Hash<string>
	{
		size_t operator()(string x)
		{
			size_t hashi = 0;
			for (auto ch : x)
			{
				hashi *= 131;
				hashi += ch;
			}
			return hashi;
		}
	};
	
	template<class K, class KeyOfT=KeyOfT<K>, class Hash = Hash<K>>
	class unordered_set
	{
		

		typedef typename HashTable<K, const K, KeyOfT, Hash>::iterator iterator;
		typedef typename HashTable<K, const K, KeyOfT, Hash>::const_iterator const_iterator;
	public:

		iterator begin()
		{
			return _hashtable.begin();
		}

		iterator end()
		{
			return _hashtable.end();
		}
		

		const_iterator begin() const
		{
			return _hashtable.begin();
		}

		const_iterator end() const
		{
			return _hashtable.end();
		}
		


		pair<iterator, bool> insert(const K& data)
		{

			return _hashtable.insert(data);
		}

		iterator find(const K& data)
		{
			return _hashtable.find(data);
		}

		iterator erase(const K& data)
		{
			return _hashtable.erase(data);
		}
		size_t size()const { return _hashtable.size(); }//数据个数
		bool empty()const { return _hashtable.empty(); }//表是否为空

		size_t bucket_count() { return _hashtable.BucketCount(); }//哈希桶的个数
		size_t bucket_size(const K& key) { return _hashtable.BucketSize(key); }//根据key查找属于哪个哈希桶

		size_t count(const K& key)//查找元素有返回1无返回0
		{
			return _hashtable.find(key) != end();
		}
		
	private:
		HashTable<K, const K, KeyOfT, Hash> _hashtable;
	};

}